﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoadTeam.Model;
using System.Data;
using RoadTeam.Enums;
using RoadTeam.SysInterface;
using RoadTeam.Pub;

namespace RoadTeam.BLL
{
    public class CsAcceptItemBLL : BllBase
    {
        public bool Insert(DataTable dt, string StrJson)
        {
            base.InserLog(StrJson);

            DataTable dt_AcceptItem = FactoryClass.CreateCsAcceptItem().GetEmptyDataFromSystem();
            DataRow dr_AcceptItem;

            DataTable dt_RepairRecord = FactoryClass.CreateCsRepairRecord().GetEmptyDataRepairRecords();
            DataRow dr_RepairRecord;

            bool flag;

            String InspectHouseId = String.Empty;
            String InspectHouseCode = String.Empty;
            int ItemID;

            DataRow Temp;
            foreach (DataRow dr in dt.Rows)
            {
                flag = false;
                //ID,收楼维修id,项目id,APP标识量,项目名称,项目内容,是否合格,收楼说明
                dr_AcceptItem = dt_AcceptItem.NewRow();
                Temp = FactoryClass.CreateCsInspectHouse().GetInspectHouseIDFromSystem(dr["InspectHouseIdentifier"].ToString());
                if (Temp[0] != null && Temp[1] != null)
                {
                    InspectHouseId = Temp[0].ToString();
                    InspectHouseCode = Temp[1].ToString();
                }
                dr_AcceptItem[1] = InspectHouseId;
                ItemID = FactoryClass.CreateCsListItem().GetCsListItemIDFromSystem(dr["ListItemIdentifier"].ToString());
                dr_AcceptItem[2] = ItemID;
                dr_AcceptItem[3] = CreateIdentifier.GetIdentifier();
                dr_AcceptItem[4] = dr["AcceptItemName"];
                dr_AcceptItem[5] = dr["AcceptItemContent"];
                dr_AcceptItem[6] = dr["AcceptItemIsQualified"];
                dr_AcceptItem[7] = dr["AcceptItemSituation"];
                dt_AcceptItem.Rows.Add(dr_AcceptItem);

                if (dr["AcceptItemIsQualified"] != null)
                {
                    bool.TryParse(dr["AcceptItemIsQualified"].ToString(), out flag);
                    if (!flag)
                    {
                        //收楼维修单号,维修项目id,施工人,维修单位,规定日期,预警日期
                        dr_RepairRecord = dt_RepairRecord.NewRow();
                        dr_RepairRecord[0] = InspectHouseCode;
                        dr_RepairRecord[1] = ItemID;
                        dr_RepairRecord[2] = dr["AcceptItemCharge"];
                        dr_RepairRecord[3] = dr["AcceptItemChargeUnit"];
                        dr_RepairRecord[4] = dr["AcceptItemFinishDate"];
                        dr_RepairRecord[5] = dr["AcceptItemFinishDate"];
                        dr_RepairRecord[6] = false;
                        dt_RepairRecord.Rows.Add(dr_RepairRecord);
                    }
                }
            }


            FactoryClass.CreateCsAcceptItem().InsertDataTableToSysTem(dt_AcceptItem);

            if (dt_RepairRecord.Rows.Count > 0)
            {
                FactoryClass.CreateCsRepairRecord().InsertDatatableToSysTem(dt_RepairRecord);
            }



            return CsAcceptItem.InsertDatatable(dt, GlobalConfig.ConnectionString);
        }


        /// <summary>
        /// 获取空表结构
        /// </summary>
        /// <returns></returns>
        public DataTable GetCsAcceptItemForEnptyTable()
        {
            SqlWhere where = new SqlWhere();
            CsAcceptItem item = new CsAcceptItem();
            item.AcceptItemIdentifier = "-1";
            where.Info = item;
            return CsAcceptItem.Select(where, GlobalConfig.ConnectionString);
        }
    }
}
